package com.fuqi.android.zhongcai.base.view.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.result.GetStaffResult;
import com.fuqi.android.zhongcai.sql.Worker;
import com.fuqi.android.zhongcai.utils.PinyinUtils;

import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by juner on 2016/7/21.
 */
public class CityListChooseAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater inflater;
    private List<Worker> mWorkers;
    private ArrayList<Worker> selWorkers;
    private HashMap<String, Integer> letterIndexes;
    private String[] sections;
    private OnCityClickListener onCityClickListener;

    public CityListChooseAdapter(Context mContext, List<Worker> mWorkers) {
        this.mContext = mContext;
        this.mWorkers = mWorkers;
        this.selWorkers = new ArrayList<Worker>();
        this.inflater = LayoutInflater.from(mContext);
        if (mWorkers == null){
            mWorkers = new ArrayList<>();
        }
        int size = mWorkers.size();
        letterIndexes = new HashMap<>();
        sections = new String[size];
        for (int index = 0; index < size; index++){
            //当前拼音首字母
            String currentLetter = PinyinUtils.getFirstLetter(mWorkers.get(index).getPinyin());
            //上个首字母，如果不存在设为""
            String previousLetter = index >= 1 ? PinyinUtils.getFirstLetter(mWorkers.get(index - 1).getPinyin()) : "";
            if (!TextUtils.equals(currentLetter, previousLetter)){
                letterIndexes.put(currentLetter, index);
                sections[index] = currentLetter;
            }
        }
    }


    /**
     * 获取字母索引的位置
     * @param letter
     * @return
     */
    public int getLetterPosition(String letter){
        Integer integer = letterIndexes.get(letter);
        return integer == null ? -1 : integer;
    }


    @Override
    public int getCount() {
        return mWorkers == null ? 0: mWorkers.size();
    }

    @Override
    public Worker getItem(int position) {
        return mWorkers == null ? null : mWorkers.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        CityViewHolder holder;
                if (view == null){
                    view = inflater.inflate(R.layout.item_worker_choose_listview, parent, false);
                    holder = new CityViewHolder();
                    holder.letter = (TextView) view.findViewById(R.id.lv_worker_letter);
                    holder.name = (TextView) view.findViewById(R.id.lv_worker_name);
                    view.setTag(holder);
                }else{
                    holder = (CityViewHolder) view.getTag();
                }
                    final Worker worker = mWorkers.get(position);
                    holder.name.setText(worker.getName());
                    String currentLetter = PinyinUtils.getFirstLetter(worker.getPinyin());
                    String previousLetter = position >= 1 ? PinyinUtils.getFirstLetter(mWorkers.get(position - 1).getPinyin()) : "";
                    if (!TextUtils.equals(currentLetter, previousLetter)){
                        holder.letter.setVisibility(View.VISIBLE);
                        holder.letter.setText(currentLetter);
                    }else{
                        holder.letter.setVisibility(View.GONE);
                    }
                    holder.name.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (onCityClickListener != null){
                                onCityClickListener.onItemClick(worker);
                                if (mWorkers.get(position).isSelected()){
                                    mWorkers.get(position).setSelected(false);
                                    selWorkers.remove(mWorkers.get(position));
                                } else {
                                    selWorkers.add(mWorkers.get(position));
                                    mWorkers.get(position).setSelected(true);
                                }

                                notifyDataSetChanged();
                            }
                        }
                    });

        if (mWorkers.get(position).isSelected()){

            holder.name.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_selected, 0, 0, 0);

        }else {

            holder.name.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_unselect, 0, 0, 0);
        }
        return view;
    }

    public static class CityViewHolder{
        TextView letter;
        TextView name;
    }

    public void setOnCityClickListener(OnCityClickListener listener){
        this.onCityClickListener = listener;
    }

    public interface OnCityClickListener{
        void onItemClick(Worker worker);
    }


    public ArrayList<Worker> getmWorkers() {

        return selWorkers;
    }


    public void loadMoreData(GetStaffResult[] getStaffResult) {

        for (int i = 0; i < getStaffResult.length; i++) {

            Worker worker = null;
            try {
                worker = new Worker(getStaffResult[i].getUserId(), getStaffResult[i].getName(), PinyinUtils.toPinYin(getStaffResult[i].getName()),getStaffResult[i].getPhone());
            } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                badHanyuPinyinOutputFormatCombination.printStackTrace();
            }
            mWorkers.add(worker);
//            SQLDao.getInstance(mContext).insertWorker(worker);
        }
        notifyDataSetChanged();

    }




}
